package com.aha.dataanalyzeandview.utils.datasource;


import com.aha.dataanalyzeandview.config.datasource.MysqlQueryMetadataSqlConfig;
import com.aha.dataanalyzeandview.entity.metadata.ColumnInfoEntity;
import com.aha.dataanalyzeandview.entity.metadata.ConnectionInfoEntity;
import com.aha.dataanalyzeandview.utils.MyJdbcUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MysqlUtils {

    /**
     * 测试MySQL的连接
     * @return
     */
    public static boolean testConn(ConnectionInfoEntity connectionInfo) {
        try {
            return listDatabases(connectionInfo).size() > 0;
        }catch (Exception e ){
            log.error("mysql 连接失败， 连接信息：" + connectionInfo.toString());
            return false;
        }
    }


    /**
     * 查询MySQL的数据库
     * @param connectionInfo
     * @return
     */
    public static List<List<String>> listDatabases(ConnectionInfoEntity connectionInfo){
        String sql = MysqlQueryMetadataSqlConfig.showDatabaseSql();

        List<List<String>> res = MyJdbcUtils.executeQuery(
                connectionInfo.getUrl(),
                connectionInfo.getDriverName(),
                "mysql",
                connectionInfo.getUserName(),
                connectionInfo.getPassword(),
                null,
                sql,
                1
        );
        return  res ;
    }

    /**
     * 列出MySQL表的所有表名
     * @param connectionInfo
     * @param dbName
     * @return
     */
    public static List<List<String>> listTables(ConnectionInfoEntity connectionInfo,String dbName){
        String sql = MysqlQueryMetadataSqlConfig.showTablesSql(dbName);

        List<List<String>> res = MyJdbcUtils.executeQuery(
                connectionInfo.getUrl(),
                connectionInfo.getDriverName(),
                dbName,
                connectionInfo.getUserName(),
                connectionInfo.getPassword(),
                null,
                sql,
                1
        );
        return  res ;
    }

    /**
     * 列出MySQL表的所有的字段的详细信息
     * @param connectionInfo
     * @param dbName
     * @param tableName
     * @return
     */
    public static List<ColumnInfoEntity> listTableFieldsDetails(ConnectionInfoEntity connectionInfo, String dbName, String tableName){
        String sql = MysqlQueryMetadataSqlConfig.queryAllColumnSql(dbName,tableName);

        List<List<String>> res = MyJdbcUtils.executeQuery(
                connectionInfo.getUrl(),
                connectionInfo.getDriverName(),
                dbName,
                connectionInfo.getUserName(),
                connectionInfo.getPassword(),
                null,
                sql,
                3
        );

        List<ColumnInfoEntity> res2 = new ArrayList<>();
        for (List<String> list : res) {
            ColumnInfoEntity columnInfoEntity = new ColumnInfoEntity();
            columnInfoEntity.setColumnName(list.get(0));
            columnInfoEntity.setColumnType(list.get(1));
            columnInfoEntity.setComment(list.get(2));
            res2.add(columnInfoEntity);
        }
        return res2;
    }

    /**
     * 列出MySQL表的所有的字段名
     * @param connectionInfo
     * @param dbName
     * @param tableName
     * @return
     */
    public static List<String> listTableFields(ConnectionInfoEntity connectionInfo, String dbName, String tableName){
        String sql = MysqlQueryMetadataSqlConfig.queryAllColumnSql(dbName,tableName);

        List<List<String>> res = MyJdbcUtils.executeQuery(
                connectionInfo.getUrl(),
                connectionInfo.getDriverName(),
                dbName,
                connectionInfo.getUserName(),
                connectionInfo.getPassword(),
                null,
                sql,
                1
        );

        List<String> columnNameList = new ArrayList<>();

        for (List<String> list : res) {
            columnNameList.add(list.get(0));
        }

        return columnNameList;
    }


//    public static void main(String[] args) {
//
//        ConnectionInfoEntity connectionInfoEntity = new ConnectionInfoEntity();
//        connectionInfoEntity.setUrl("jdbc:mysql://localhost:3306");
//        connectionInfoEntity.setUserName("root");
//        connectionInfoEntity.setPassword("123456");
//        connectionInfoEntity.setDriverName("com.mysql.jdbc.Driver");
//
//        System.out.println(testConn(connectionInfoEntity));
//    }


}
